package com.maishi.programmer.singleton;

/**
 * Created by brossdong on 2018/3/11.
 * 单例模式
 */
public class Singleton {

    /**
     * 饿汉式单例  一
     */
    private static final Singleton instance = new Singleton();
    //私有构造函数
    private Singleton(){};
    public static Singleton getInstance() {
        return instance;
    }


    /**
     * 懒汉式单例
     */
    private static Singleton instance1 = null;
    //私有构造函数
    public synchronized static Singleton getInstance1() {
        if (instance1 == null) {
            instance1 = new Singleton();
        }
        return instance1;
    }


    /**
     * 懒汉式单例  二(改版) 保证同步性
     */
    private static class SingletonHoder {
        private static final Singleton instance3 = new Singleton();
    }
    //私有构造函数
    public static final Singleton getInstance3() {
        return SingletonHoder.instance3;
    }

}
